//index.js
//获取应用实例
const app = getApp()
const mineService = require('../../service/mine.js');

Page({
  data: {
    title:"我的主页",
    unionId:"",
    detailinfo:{},
    currentTab:2,
    list: [],
    queryParams: {
      page: 1,
      num: 10
    },
    isLast: false,
    isArticleLast:false,
    followname: '关注作者',
    followbindtap: 'fansFollow',
    queryArticleParams:{
      page: 1,
      num: 10
    }
  },
  onLoad: function (options) {
    this.setData({
      unionId: options.unionId || app.globalData.userinfo.unionId
    });
  },
  onShow(){
    this.getDetail();
    this.getContentList();
  },
  switchTab(e){
    let currentTab = e.target.dataset.tab;
    this.setData({
      currentTab
    });
    if (currentTab == 2 && !this.data.list.length) {
      this.getContentList();
    }
  },
  getDetail(){
    let d = {
      userId: this.data.unionId
    }
    mineService.getCustomerIndex(d).then(res => {
      if(res.resCode == 0){
        this.setData({
          detailinfo:res.object,
          title:res.object.user.nickName
        })
      }
    })
  },
  getContentList() {
    let d = {
      ...this.data.queryParams,
      unionId:this.data.unionId
    }
    mineService.getCustomerDynamic(d).then(res => {
      if (res.resCode == 0) {
        if (!res.object.length) {
          this.setData({
            isLast: true
          });
          return false;
        }
        let d = this.data.list.concat(res.object);
        this.setData({
          list: d,
        })
        this.waterfallComponent = this.selectComponent("#waterfall");
        if (!this.data.list.length) {
          this.waterfallComponent.start(res.object).then(res => {
          })
        } else {
          this.waterfallComponent.append(res.object).then(res => { })
        }
      }
    })
  },
  getArticleList(){
    let d = {
      ...this.data.queryArticleParams,
      unionId: this.data.unionId
    }
    mineService.getCustomerArticle(d).then(res => {
      if (res.resCode == 0) {
        if (!res.object.length) {
          this.setData({
            isArticleLast: true
          });
          return false;
        }
        let d = this.data.detailinfo.paper.concat(res.object);
        this.setData({
          "detailinfo.paper": d,
        })
      }
    })
  },
  onReachBottom() {
    //区分触底情况
    let currentTab = this.data.currentTab;
    let statusFactory = {
      1:{
        lastKey:"isArticleLast",
        currentTabPage:"queryArticleParams",
        fn:"getArticleList"

      },
      2:{
        lastKey:"isLast",
        currentTabPage:"queryParams",
        fn:"getContentList"
      }
    }
    let config = statusFactory[currentTab];
    if (this.data[config.lastKey]) return false;
    let key = config["currentTabPage"] + ".page"
    this.setData({
      key: ++this.data[config.currentTabPage].page,
    });
    this[config.fn]();
  },
  fansFollow() {
    if (!app.checkLogin()) return false;
    let d = {
      follower: this.data.item.unionId,
      objectType: 0,
      unionId: app.globalData.userinfo.unionId
    };
    console.log(d);
    mineService.fansFollow(d).then(res => {
      if (res.resCode == 0) {
        wx.showToast({
          title: '关注成功',
          icon: "none"
        })
        this.setData({
          followname: '已关注',
          followbindtap: ''
        });
      }
    })
  }
  
})
